package safe.yunchexing.com.cloudsafe.utils.app;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * 
 * @describe 应用程序包工具类.
 */
public class PackageUtils {
    /**
     * 得到Application的指定的KEY的MetaData信息
     * 
     * @param metaDataKey
     * @return
     */
    public static String getApplicationMetadata(Context context,String metaDataKey) {
        ApplicationInfo info = null;
        try {
            PackageManager pm = context.getPackageManager();

            info = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

            return String.valueOf(info.metaData.get(metaDataKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 
     * getPackageName:得到应用包名. <br/>
     * @return
     */
    public static String getPackageName(Context context){
        return context.getPackageName();
    }
    /**
     * 
     * getVersionName:得到版本名称. <br/>
     * @return
     */
    public static String getVersionName(Context context){
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "1.0";
    }
    /**
     * 
     * getVersionCode:得到版本号. <br/>
     * @return
     */
    public static int getVersionCode(Context context){
        
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

}
